package test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
* @author 作者 linweiqiang
* @version 创建时间：2017年11月14日 上午10:48:11
* 类说明
*/
public class MyJsoupHtml {
	/**
	 * 解析html 返回img的url
	 * @param html
	 * @return
	 */
	public static List<String> getImgUrl(String html){
		List<String> list = new ArrayList<String>();
		Document document = Jsoup.parse(html);
		Elements imgs = document.select("img[src]");
		Iterator<Element> it = imgs.iterator();
		while(it.hasNext()){
			Element el = it.next();
			String imgSrc = el.attr("src");
			list.add(imgSrc);
		}
		return list;
	}
	
	public static List<String> getAllUrl(String html){
		List<String> list = new ArrayList<String>();
//		Document document = Jsoup.parse(html);
		Document document;
		try {
//			document = Jsoup.connect("http://bbs.csdn.net/topics/391115877").get();
//			document = Jsoup.connect("https://p.baidu.com/daily/view?id=99386").get();
			document = Jsoup.connect("http://www.cs1.xin").get();
			System.out.println(document.html());
			Elements imgs = document.getAllElements();
			Iterator<Element> it = imgs.iterator();
			while(it.hasNext()){
				Element el = it.next();
				String imgSrc = el.attr("abs:src");
				if(StringUtils.isBlank(imgSrc))
					continue;
				list.add(imgSrc.replace("\\", "/").replaceAll("\\.\\./", ""));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(list.toString());
		return list;
	}
	public static void main(String[] args) {
		getAllUrl("");
	}
}
